package com.lorenzogao.pley.ui.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lorenzogao.pley.R
import com.lorenzogao.pley.bean.AppInfo
import com.lorenzogao.pley.ui.widget.DownloadButtonConntroller
import com.lorenzogao.pley.ui.widget.DownloadProgressButton
import zlc.season.rxdownload2.RxDownload

/**
 * 作者：Lorenzo Gao
 * Date: 2018/1/4
 * Time: 14:03
 * 邮箱：2508719070@qq.com
 * Description:
 */
class AppInfoAdapter(builder: Builder) : BaseQuickAdapter<AppInfo, BaseViewHolder>(builder.layoutId) {

    private var baseImgUrl = "http://file.market.xiaomi.com/mfc/thumbnail/png/w150q80/"

    private var mBuilder: Builder = builder


    private var mDownloadButtonConntroller: DownloadButtonConntroller = DownloadButtonConntroller(builder.mRxDownload!!)

    override fun convert(helper: BaseViewHolder, item: AppInfo) {

        var img_app_icon = helper.getView(R.id.img_app_icon) as ImageView
        Glide.with(img_app_icon.context).load(baseImgUrl + item.icon).into(img_app_icon)
        helper.setText(R.id.txt_app_name, item.displayName)

        helper.addOnClickListener(R.id.btn_download)
        var btn = helper.getView(R.id.btn_download) as View


        var txtPosition = helper.getView(R.id.txt_position) as TextView?
        if (txtPosition != null) {
            txtPosition.visibility = if (mBuilder.inShowPosition) View.VISIBLE else View.GONE
            txtPosition.text = (item.position + 1).toString() + "."
        }


        var tvCategoryName = helper.getView(R.id.txt_category) as TextView?
        if (tvCategoryName != null) {
            tvCategoryName.visibility = if (mBuilder.inShowCategoryName) View.VISIBLE else View.GONE
            tvCategoryName.text = item.level1CategoryName
        }


        var txtBrief = helper.getView(R.id.txt_brief) as TextView?
        if (txtBrief != null) {
            txtBrief.visibility = if (mBuilder.isShowBrief) View.VISIBLE else View.GONE
            txtBrief.text = item.briefShow
        }

        var tvSize = helper.getView(R.id.txt_apk_size) as TextView?
        if (tvSize != null) {
            tvSize.text = (item.apkSize / 1024 / 1024).toString() + "MB"
        }




        if (btn is DownloadProgressButton) {

            mDownloadButtonConntroller.handClick(btn, item)
        }


    }


    companion object {

        fun builder(): Builder {

            return Builder()
        }


        class Builder {

            var inShowPosition: Boolean = false
            var inShowCategoryName: Boolean = false
            var isShowBrief: Boolean = false

            var mRxDownload: RxDownload? = null

            var layoutId = R.layout.template_appinfo

            fun showPosition(boolean: Boolean): Builder {
                this.inShowPosition = boolean
                return this
            }

            fun ShowCategoryName(boolean: Boolean): Builder {
                this.inShowCategoryName = boolean
                return this
            }

            fun ShowBrief(boolean: Boolean): Builder {
                this.isShowBrief = boolean
                return this
            }

            fun build(): AppInfoAdapter {
                return AppInfoAdapter(this)
            }

            fun layout(resId: Int): Builder {
                this.layoutId = resId
                return this
            }

            fun rxDownload(rxDownload: RxDownload): Builder {
                this.mRxDownload = rxDownload
                return this
            }

        }
    }

}